package com.example.demo.controller;

import com.example.demo.entity.commodity;
import com.example.demo.entity.inventory;
import com.example.demo.service.commodityserviceInterface;
import com.example.demo.service.inventoryserviceInter;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/commodity")
public class commoditycontroller {
    @Autowired
    commodityserviceInterface commoditydao;
    @Autowired
    inventoryserviceInter inventorydao;

    @RequestMapping("/display")
    public PageInfo<commodity> display(@RequestParam(value = "pagenum", required = false, defaultValue = "1")Integer pagenum,
                          @RequestParam(value = "pagesize", required = false, defaultValue = "5")Integer pagesize,
                          Model model) {
        PageInfo<commodity> comopageinfo = commoditydao.display(pagenum, pagesize);
        model.addAttribute("comopageinfo", comopageinfo);

        return comopageinfo;
    }

    @RequestMapping("/insert")
    public int insert(commodity c) {
        return commoditydao.insert(c);
    }

    @RequestMapping("/update")
    public int update(commodity c) {
        return commoditydao.update(c);
    }

    @RequestMapping("/delete")
    public int delete(commodity c) {
        return commoditydao.delete(c);
    }

    @RequestMapping("/search")
    public  List<commodity> search(commodity c) {
        List<commodity> como = new ArrayList<>();

        como.addAll(commoditydao.search(c));

        Iterator<commodity> ite = como.iterator();

        while (ite.hasNext()) {
            System.out.println(ite.next());

//            if (ite.next().getDelmark() == 1) {
//                ite.remove();
//                continue;
//            }
        }

        return como;
    }

    @RequestMapping("/verify")
    public int verify(commodity c) {
        int bool=0, bool1=0, bool2=0, bool3=0;
        inventory in = new inventory();
        in.setInventoryid(c.getCommodityid());
        in.setInventoryname(c.getCommodityname());
        in.setInventoryprice(c.getCommodityprice());
        in.setInventorynum(c.getCommoditynum());
        in.setDataofproduced(c.getDataofproduced());
        in.setDatabaozhi(c.getDatabaozhi());
        in.setManufactory(c.getManufacturer());
        in.setInventoryaddress(c.getCommodityaddress());


        bool2 = inventorydao.updatenum(in);

        if (bool2 == 1) {
            bool1 = commoditydao.verify(c);
        }
        else {
            bool1 = commoditydao.verify(c);
            bool3 = inventorydao.insertcommodity(in);
        }

        if (bool1 == 1 && (bool2==1 || bool3==1)) {
            bool = 1;
        }

        return bool;
    }
}
